package com.codeczx.colordemo;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.SeekBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProgressActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private static final String TAG = "ProgressActivity";
    private static int MAX_VALUE = 255;
    private static int MID_VALUE = 127;

    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.seekbar_hue)
    SeekBar seekbarHue;
    @BindView(R.id.seekbar_saturation)
    SeekBar seekbarSaturation;
    @BindView(R.id.seekbar_lum)
    SeekBar seekbarLum;

    private float mHue = 0;
    private float mSaturatuion = 0;
    private float mLum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        ButterKnife.bind(this);

        seekbarHue.setOnSeekBarChangeListener(this);
        seekbarSaturation.setOnSeekBarChangeListener(this);
        seekbarLum.setOnSeekBarChangeListener(this);

        seekbarHue.setMax(MAX_VALUE);
        seekbarHue.setProgress(MID_VALUE);

        seekbarSaturation.setMax(MAX_VALUE);
        seekbarSaturation.setProgress(MID_VALUE);

        seekbarLum.setMax(MAX_VALUE);
        seekbarLum.setProgress(MID_VALUE);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.seekbar_hue:
                mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;
                break;
            case R.id.seekbar_saturation:
                mSaturatuion = progress * 1.0F / MID_VALUE;
                break;
            case R.id.seekbar_lum:
                mLum = progress * 1.0f / MID_VALUE;
                break;
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        imageView.setImageBitmap(ImageHelper.handleImageEffect(BitmapFactory.decodeResource(getResources(), R.drawable.girl), mHue, mSaturatuion, mLum));
        Log.i(TAG, "onStopTrackingTouch: " + mHue + "  " + mSaturatuion + "  " + mLum);
    }
}
